//
//  YAYIMutiPracticeDetailViewController.m
//  YAYIDoctor
//
//  Created by Veer on 16/6/22.
//
//

#import "YAYIMutiPracticeDetailViewController.h"

#import "YAYIScrolImageView.h"
#import "YAYIRecruitHospitalModel.h"
#import "NSString+Tools.h"


@interface YAYIMutiPracticeDetailViewController () <UIScrollViewDelegate>

@property (nonatomic, assign) CGRect desImageViewRect;
@property (nonatomic, copy) UIImage *image;

@property (nonatomic, weak) YAYIScrolImageView *scrollImageView;
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, weak) UIView *containView;
@property (nonatomic, weak) UIButton *reserveBtn;

@property (nonatomic, weak) UIView *lastView;

@end

@implementation YAYIMutiPracticeDetailViewController


- (instancetype)initWithModel:(UIImage *)image desImageViewRect:(CGRect)desRect{
    if (self = [super init]) {
        self.desImageViewRect = desRect;
        self.image = image;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"职位详情";
    
    self.view.backgroundColor = [UIColor clearColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupNav];
    [self setupSubView];
    [self setupHospitalDetailView];
//    [self getRecruitHospitalDetail];
}


- (void)setupNav
{
    // 改写导航栏返回按钮执行方法
    UIButton *backBtn = self.navigationItem.leftBarButtonItem.customView;
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}


- (void)setupSubView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.alwaysBounceVertical = YES;
    scrollView.delegate = self;
    scrollView.frame = CGRectMake(0, 0, SCREEN_width, SCREEN_height);
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    CGRect rect = self.desImageViewRect;
    rect.origin.y = 0;
    UIView *headerView = [[UIView alloc] initWithFrame:self.desImageViewRect];
    headerView.frame = self.desImageViewRect;
    [scrollView addSubview:headerView];
    
    
    YAYIScrolImageView *scrollImageView = [[YAYIScrolImageView alloc] initWithFrame:rect];
    scrollImageView.isAutoScroll = NO;
    [headerView addSubview:scrollImageView];
    self.scrollImageView = scrollImageView;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [scrollImageView addGestureRecognizer:tap];
    
    
    UIView *containView = [[UIView alloc] init];
    containView.backgroundColor = [UIColor whiteColor];
    containView.frame = CGRectMake(0, SCREEN_height, SCREEN_width, 300);
    [scrollView addSubview:containView];
    self.containView = containView;
    
    // 底部浮动按钮
    UIButton *reserveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [reserveBtn setTitle:@"立即预约" forState:UIControlStateNormal];
    [reserveBtn setTitle:@"已投递" forState:UIControlStateDisabled];
    reserveBtn.backgroundColor = YAYIColor(83,165, 243);
    if (self.recruitHospitalModel.hassend == 2) {
        reserveBtn.enabled = NO;
        reserveBtn.backgroundColor = [UIColor lightGrayColor];
    }
    [reserveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [reserveBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    reserveBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    reserveBtn.frame = CGRectMake(0, SCREEN_height, SCREEN_width, 40);
    [self.view addSubview:reserveBtn];
    self.reserveBtn = reserveBtn;
    [reserveBtn addTarget:self action:@selector(onAppointClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [UIView animateWithDuration:0.3 animations:^{
        
        containView.y = CGRectGetMaxY(self.desImageViewRect);
        reserveBtn.y = SCREEN_height - 40;
    }];
}


- (void)setupHospitalDetailView
{
    UIView *topView = [[UIView alloc] init];
    [self.containView addSubview:topView];
    topView.frame = CGRectMake(0, 0, SCREEN_width, 70);
    
    //  医院信息label
    UILabel *hospitalNameLbl = [[UILabel alloc] init];
    hospitalNameLbl.text = self.recruitHospitalModel.hospitalname;
    hospitalNameLbl.font = [UIFont systemFontOfSize:16];
    [topView addSubview:hospitalNameLbl];
    [hospitalNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(10);
    }];
    
    UILabel *addressLbl = [[UILabel alloc] init];
    addressLbl.text = self.recruitHospitalModel.hosaddress;
    addressLbl.numberOfLines = 0;
    addressLbl.font = [UIFont systemFontOfSize:14];
    addressLbl.textColor = YAYIColor(156, 156, 156);
    [topView addSubview:addressLbl];
    [addressLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_lessThanOrEqualTo(-70);
        make.top.mas_equalTo(hospitalNameLbl.mas_bottom).offset(5);
    }];
    
    UILabel *distanceLbl = [[UILabel alloc] init];
    distanceLbl.text = [self getDistanceStringByDistance:self.recruitHospitalModel.distance];
    distanceLbl.font = [UIFont systemFontOfSize:14];
    distanceLbl.textColor = YAYIColor(156, 156, 156);
    [topView addSubview:distanceLbl];
    [distanceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(addressLbl);
    }];
    
    // 合作模式 1.日租 2.分成
    UILabel *cooperationModelLbl = [[UILabel alloc] init];
    cooperationModelLbl.font = [UIFont systemFontOfSize:14];
    cooperationModelLbl.textColor = YAYIColor(96, 167, 237);
    //    cooperationModelLbl.text = @"日租";
    cooperationModelLbl.textAlignment = NSTextAlignmentCenter;
    cooperationModelLbl.layer.borderWidth = 1.0;
    cooperationModelLbl.layer.borderColor = YAYIColor(96, 167, 237).CGColor;
    cooperationModelLbl.layer.cornerRadius = 5.0;
    cooperationModelLbl.layer.masksToBounds = YES;
    cooperationModelLbl.text = (self.recruitHospitalModel.mode == 1) ? @"日租" : @"分成";
    if (self.recruitHospitalModel.mode == 1) {
        cooperationModelLbl.text = @"日租";
    }else{
        cooperationModelLbl.text = [NSString stringWithFormat:@"%zd%%分成", self.recruitHospitalModel.price];
    }
    
    CGSize size = [cooperationModelLbl.text sizeWithFont:cooperationModelLbl.font];
    [topView addSubview:cooperationModelLbl];
    [cooperationModelLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(hospitalNameLbl).offset(0);
        make.width.mas_equalTo(size.width + 12);
        make.height.mas_equalTo(20);
    }];
    
    // 计算地址的高度
    CGSize addressSize = [addressLbl.text sizewithFont:addressLbl.font maxSize:CGSizeMake(SCREEN_width - 15 - 70, CGFLOAT_MAX)];
    CGSize hosSize = [hospitalNameLbl.text sizeWithFont:hospitalNameLbl.font];
    CGFloat topViewH = 10 + hosSize.height + 5 + addressSize.height + 10;
    topView.height = topViewH;
    
    // 线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = YAYIColor(230, 230, 230);
    [topView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(topView);
        make.width.mas_equalTo(SCREEN_width);
        make.height.mas_equalTo(0.5);
    }];
    
    
    // 标题
    UILabel *titleLbl1 = [[UILabel alloc] init];
    titleLbl1.text = @"医院简介";
    titleLbl1.font = [UIFont systemFontOfSize:16];
    titleLbl1.textColor = YAYIColor(96, 167, 237);
    [self.containView addSubview:titleLbl1];
    [titleLbl1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(topView.mas_bottom).offset(5);
    }];
    
    UILabel *briefLbl = [[UILabel alloc] init];
    briefLbl.text = self.recruitHospitalModel.brief;
    briefLbl.numberOfLines = 0;
    briefLbl.font = [UIFont systemFontOfSize:14];
    briefLbl.textColor = YAYIColor(141, 142, 143);
    [self.containView addSubview:briefLbl];
    [briefLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(titleLbl1.mas_bottom).offset(8);
        make.right.mas_equalTo(-15);
    }];
    
    
    // 合作信息
    UILabel *titleLbl2 = [[UILabel alloc] init];
//    titleLbl2.text = @"合作信息";
    titleLbl2.text = self.recruitHospitalModel.title;
    titleLbl2.font = [UIFont systemFontOfSize:16];
    titleLbl2.textColor = YAYIColor(96, 167, 237);
    [self.containView addSubview:titleLbl2];
    [titleLbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(briefLbl.mas_bottom).offset(10);
    }];
    
    UILabel *jobLbl = [[UILabel alloc] init];
    jobLbl.text = self.recruitHospitalModel.jobtitle;
    jobLbl.numberOfLines = 0;
    jobLbl.font = [UIFont systemFontOfSize:14];
    jobLbl.textColor = YAYIColor(141, 142, 143);
    [self.containView addSubview:jobLbl];
    [jobLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(titleLbl2.mas_bottom).offset(8);
        make.right.mas_equalTo(-12);
    }];
    
    // 标题
//    UILabel *titleLbl = [[UILabel alloc] init];
//    titleLbl.text = self.recruitHospitalModel.title;
//    titleLbl.font = [UIFont systemFontOfSize:15];
//    titleLbl.textColor = YAYIColor(96, 167, 237);
//    [self.containView addSubview:titleLbl];
//    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(12);
//        make.top.mas_equalTo(jobLbl.mas_bottom).offset(10);
//    }];

    self.lastView = jobLbl;
}


- (void)viewDidLayoutSubviews
{
    YAYILog(@"");
    
//    self.containView.height = self.lastView.y + self.lastView.height;
//    self.scrollView.contentSize = CGSizeMake(SCREEN_width, 64 + self.containView.height + self.desImageViewRect.size.height);
    
    
}


#pragma mark - 获取数据
//- (void)getRecruitHospitalDetail
//{
//    
//    NSMutableDictionary *param = [NSMutableDictionary dictionary];
//    param[@"recruitid"] = self.recruitid;
//    
//    [YAYINetWorking getWithURL:docPpHosList_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
//        
//        YAYILog(@"返回:%@", dict);
//        
//        
//    }];
//}


#pragma mark - 点击事件
- (void)back
{
    [SVProgressHUD dismiss];
    
    self.view.backgroundColor = [UIColor clearColor];
    
    self.scrollImageView.hidden = YES;
    __block typeof(self) weakself = self;
    [UIView animateWithDuration:0.3 animations:^{
        
        weakself.containView.y = SCREEN_height;
        self.reserveBtn.y = SCREEN_height;
    }];
}


- (void)handleTapGesture:(UITapGestureRecognizer *)recognizer
{
    
    self.view.backgroundColor = [UIColor clearColor];
    
    self.scrollImageView.hidden = YES;
    __block typeof(self) weakself = self;
    [UIView animateWithDuration:0.3 animations:^{
        
        weakself.containView.y = SCREEN_height;
        self.reserveBtn.y = SCREEN_height;
    }];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)onAppointClick:(UIButton *)sender
{
        
    self.view.backgroundColor = [UIColor clearColor];
    self.scrollImageView.hidden = YES;
    __block typeof(self) weakself = self;

    [UIView animateWithDuration:0.3 animations:^{
        
        weakself.containView.y = SCREEN_height;
        self.reserveBtn.y = SCREEN_height;
    } completion:^(BOOL finished) {
        
        // 执行代理方法
        if ([weakself.delegate respondsToSelector:@selector(mutiPracticeDetailDidNextCardCicked)]) {
            [weakself.delegate mutiPracticeDetailDidNextCardCicked];
        }
    }];
    [weakself.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 工具方法
- (NSString *)getDistanceStringByDistance:(NSInteger)distance
{
    NSString *distanceString = @"";
    
    if (distance < 1000){
        
        distanceString = [NSString stringWithFormat:@"< %.1fkm", 1.0];
    }else if (distance < 10000){
        
        CGFloat km = distance / 1000.0;
        distanceString = [NSString stringWithFormat:@"< %0.1fkm", km];
    }else{
    
        NSInteger km = (distance + 500) / 1000;
        distanceString = [NSString stringWithFormat:@"< %zdkm", km + 1];
    }
    
    
    return distanceString;
}


#pragma mark - 代理方法
-(void)didFinishTransition
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.imageView.image  = self.image;//[UIImage imageNamed:self.imageName];
    
    self.scrollView.contentSize = CGSizeMake(SCREEN_width, 2 * SCREEN_height);
    
    NSMutableArray *urlArray = [NSMutableArray array];
    for (NSString *str in self.recruitHospitalModel.images) {
        
        NSString *urlStr = [NSString stringWithFormat:@"%@%@", API, str];
        [urlArray addObject:urlStr];
    }

    self.scrollImageView.imageAry = [urlArray copy];
    
    // 计算containView的高度，和contentSize
    CGFloat contentViewH = self.lastView.y + self.lastView.height + 70;
    if (contentViewH < SCREEN_height - 64 - self.desImageViewRect.size.height) {
        
        contentViewH = SCREEN_height - 64 - self.desImageViewRect.size.height;
    }
    self.containView.height = contentViewH;
    self.scrollView.contentSize = CGSizeMake(SCREEN_width, 64 + self.containView.height + self.desImageViewRect.size.height);
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY < 0) {
        
        offsetY = ABS(offsetY);
        CGFloat scale = (self.scrollImageView.height + offsetY) / self.scrollImageView.height;
        self.scrollImageView.transform = CGAffineTransformMakeScale(scale, scale);
        self.scrollImageView.y = - offsetY;
    }
}


@end
